package com.chookss.home.audit;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.clockIn.ClockInAskHolidayDetailActivity;
import com.chookss.home.audit.clockIn.ClockInReissueCardDetailActivity;
import com.chookss.home.audit.clockIn.ClockInUtils;
import com.chookss.home.audit.project.GroupDetailActivity;
import com.chookss.home.audit.reimbursement.ReimbursementDetailActivity;
import com.chookss.home.audit.resign.EntryDetailActivity;
import com.chookss.home.audit.resign.ResignDetailActivity;
import com.chookss.home.audit.seal.SealDetailActivity;
import com.chookss.home.audit.shop.ShopDetailActivity;
import com.chookss.home.audit.shop.ShopServiceDetailActivity;
import com.chookss.home.audit.upgrade.UpgradeDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class AuditOverActivity extends BaseAct {
//我的审批列表 从待审批右上角进入，一般只有管理者才有数据
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    private ClockInMyExamineAdapter adapter;
    private List<WorkNewEntity> list = new ArrayList<>();
    private int currentPage = 1;
    private int pageSize = 20;
    private boolean isEnd = false;//是否到底
    private View emptyView;
    private TextView tvNone;//空布局;
    private int type = 3;// 1新闻2视频3补卡4-请假5-外勤6图文7报销8物料9借款10补贴11用章12维修13交付14离职15晋升16回款17修改项目金额18入职19加班

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_srlview_top);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        type = getIntent().getIntExtra("type", 3);
        if (4 == type) {
            commonTitleTxt.setText("请假审批记录");
        } else if (7 == type) {
            commonTitleTxt.setText("报销审批记录");
        } else if (8 == type) {
            commonTitleTxt.setText("物品审批记录");
        } else if (9 == type) {
            commonTitleTxt.setText("借款审批记录");
        } else if (10 == type) {
            commonTitleTxt.setText("补贴审批记录");
        } else if (11 == type) {
            commonTitleTxt.setText("用章审批记录");
        } else if (14 == type) {
            commonTitleTxt.setText("入/离职审批记录");
        }else if (15 == type) {
            commonTitleTxt.setText("晋升审批记录");
        }else if (16 == type) {
            commonTitleTxt.setText("项目管理审批记录");
        }else if (17 == type) {
            commonTitleTxt.setText("项目管理审批记录");
        }else if (18 == type) {
            commonTitleTxt.setText("入职审批记录");
        }else if (19 == type) {
            commonTitleTxt.setText("加班审批记录");
        }else {
            commonTitleTxt.setText("考勤审批记录");
        }
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) smrRf.getLayoutParams();
        params.setMargins(0, Utils.dip2px(this, 12), 0, 0);
        smrRf.setLayoutParams(params);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new ClockInMyExamineAdapter(R.layout.item_clock_examine, this, list);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerView.setAdapter(adapter);
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_common_empty, recyclerView, false);
        tvNone = emptyView.findViewById(R.id.tvEmpty);
        tvNone.setText("暂无记录");
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                isEnd = false;
                currentPage = 1;
                getData();
            }
        });
        smrRf.autoRefresh();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
//        map.put("auditStatus", "1");// 为0是查询待办 为1是全部（待办加已办） 为空（或则其他数字时）时查询已办
        map.put("pageNum", currentPage + "");
        map.put("pageSize", pageSize + "");
        // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文7报销10补贴11用章
        if (type == 3) {
            //考勤系列
            map.put("type", "3,5");
        } else if (type == 8) {
            //物品系列
            map.put("type", "8,12");
        }else if (type == 16||type == 17) {
            //项目管理
            map.put("type", "13,16,17");
        } else if (type == 14) {
            //离职晋升
            map.put("type", "14,18");
        } else {
            map.put("type", type + "");
        }
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<WorkNewEntity>>>() {
        }.getType(), null, Urls.selectKqMyWorkList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<WorkNewEntity>>() {
            @Override
            public void onSuccess(List<WorkNewEntity> data) {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                if (smrRf == null) {
                    return;
                }
                if (data != null && data.size() > 0) {
                    if (data.size() < pageSize) {
                        isEnd = true;
                    }
                    int old = list.size();
                    list.addAll(data);
                    adapter.notifyItemRangeChanged(old, data.size());
                    smrRf.setVisibility(View.VISIBLE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        adapter.setEmptyView(emptyView);
                    }
                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    class ClockInMyExamineAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
        private Activity context;

        public ClockInMyExamineAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvTime = helper.getView(R.id.tvTime);
            TextView tvStatus = helper.getView(R.id.tvStatus);
            if ( "3".equals(entity.getWorkFlowType())) {
                //打卡
                if (!Utils.isNull(entity.getParams().getShangbanBukaAuditCd())) {
                    //说明补卡的是上午
                    tvTitle.setText(entity.getProjectName() + "(上班)");
                } else {
                    tvTitle.setText(entity.getProjectName() + "(下班)");
                }
            } else {
                //请假  外勤
                if (Utils.isNull(entity.getProjectCode())) {
                    tvTitle.setText(entity.getProjectName());
                } else {
                    tvTitle.setText(entity.getProjectName() + "(" + ClockInUtils.getGroupEnd(entity.getProjectCode()) + ")");
                }
            }

            tvTime.setText(entity.getCreateTime());
            if ("0".equals(entity.getAuditStatus())) {
                tvStatus.setText("待审批");
                tvStatus.setTextColor(Color.parseColor("#FCAD23"));
            } else if ("1".equals(entity.getAuditStatus())) {
                if ("8".equals(entity.getWorkFlowType())) {
                    if ("1".equals(entity.getParams().getReceiverCd())) {
                        tvStatus.setText("交接中");
                        tvStatus.setTextColor(Color.parseColor("#FCA817"));
                    } else {
                        if ("5".equals(entity.getParams().getStatusCd())) {
                            tvStatus.setText("遗失中");
                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
                        } else if ("6".equals(entity.getParams().getStatusCd())) {
                            tvStatus.setText("报废中");
                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
                        } else if ("2".equals(entity.getParams().getStatusCd())) {
                            tvStatus.setText("遗失");
                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
                        } else if ("3".equals(entity.getParams().getStatusCd())) {
                            tvStatus.setText("报废");
                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
                        } else {
                            tvStatus.setText("审批通过");
                            tvStatus.setTextColor(Color.parseColor("#00C993"));
                        }
//                        else if ("7".equals(entity.getParams().getStatusCd())) {
//                            tvStatus.setText("交付中");
//                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
//                        } else if ("8".equals(entity.getParams().getStatusCd())) {
//                            tvStatus.setText("维修中");
//                            tvStatus.setTextColor(Color.parseColor("#F2434D"));
//                        }
                    }

                } else {
                    tvStatus.setText("审批通过");
                    tvStatus.setTextColor(Color.parseColor("#00C993"));
                }
            } else {
                if ("3".equals(entity.getAuditStatus())) {
                    if ("18".equals(entity.getWorkFlowType())) {
                        tvStatus.setText("不通过");
                    }else{
                        tvStatus.setText("废弃");
                    }
                } else {
                    tvStatus.setText("审批不通过");
                }
                tvStatus.setTextColor(context.getResources().getColor(R.color.main_color));
            }
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("4".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, ClockInAskHolidayDetailActivity.class);
                        intent.putExtra("data", entity);
                        startActivity(intent);
                    } else if ("7".equals(entity.getWorkFlowType()) || "9".equals(entity.getWorkFlowType()) || "10".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, ReimbursementDetailActivity.class);
                        intent.putExtra("data", entity);
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                        ;
                        startActivity(intent);
                    } else if ("8".equals(entity.getWorkFlowType())||"13".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, ShopDetailActivity.class);
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                        intent.putExtra("goodsCode", entity.getProjectCode());
                        startActivity(intent);
                    } else if ("11".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, SealDetailActivity.class);
                        intent.putExtra("goodsCode", entity.getProjectCode());
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                        intent.putExtra("data", entity);
                        startActivity(intent);
                    } else if ("12".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, ShopServiceDetailActivity.class);
                        intent.putExtra("repairCode", entity.getProjectCode());
                        startActivity(intent);
                    } else if ("14".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, ResignDetailActivity.class);
                        intent.putExtra("data", entity);
                        startActivity(intent);
                    } else if ("15".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, UpgradeDetailActivity.class);
                        intent.putExtra("data", entity);
                        startActivity(intent);
                    }else if ("16".equals(entity.getWorkFlowType())||"17".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(context, GroupDetailActivity.class);
                        intent.putExtra("repairCode", entity.getProjectCode());
                        intent.putExtra("type",Utils.changeNumber(entity.getWorkFlowType()));
                        startActivity(intent);
                    }else if ("18".equals(entity.getWorkFlowType())) {
                        Intent intent = new Intent(AuditOverActivity.this, EntryDetailActivity.class);
                        intent.putExtra("data", entity);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(AuditOverActivity.this, ClockInReissueCardDetailActivity.class);
                        intent.putExtra("data", entity);
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                        startActivity(intent);
                    }
                }
            });
        }
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }
}
